#!/bin/bash
# cash-kit — one command for everything. Stdlib Python only; uses the Mac's /usr/bin/python3.
set -e
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
export CASH_KIT_ROOT="${CASH_KIT_ROOT:-$HERE}"
PY=/usr/bin/python3; command -v "$PY" >/dev/null || PY=python3
case "${1:-}" in
  claim)    shift; "$PY" "$HERE/bin/simplefin_pull.py" claim "$@" ;;
  pull)     shift; "$PY" "$HERE/bin/simplefin_pull.py" pull "$@" ;;
  status)   "$PY" "$HERE/bin/simplefin_pull.py" status ;;
  today)    "$PY" "$HERE/bin/gen_today.py" ;;
  calendar) "$PY" "$HERE/bin/gen_spend_calendar.py" ;;
  ingest)   shift; "$PY" "$HERE/bin/ingest.py" "$@" ;;
  ledger)   "$PY" "$HERE/bin/gen_ledger.py" ;;
  cards)    shift
            if [ $# -eq 0 ]; then "$PY" "$HERE/bin/gen_cards.py"; else "$PY" -I "$HERE/bin/cards_cli.py" "$@"; fi ;;
  morning)  LOG="$HERE/ledger/inbox/morning.log"
            if [ -f "$LOG" ] && [ "$(wc -c < "$LOG")" -gt 200000 ]; then tail -c 100000 "$LOG" > "$LOG.tmp" && mv "$LOG.tmp" "$LOG"; fi
            "$PY" "$HERE/bin/simplefin_pull.py" pull || true
            "$PY" "$HERE/bin/gen_today.py"
            "$PY" "$HERE/bin/gen_spend_calendar.py"
            "$PY" "$HERE/bin/gen_ledger.py"
            "$PY" "$HERE/bin/gen_cards.py" ;;
  open)     open "$HERE/today.html" ;;
  install)  "$HERE/install.sh" ;;
  *) cat <<USAGE
cash-kit — your money, on your Mac, nowhere else.

  ./cash-kit claim                 one-time: link your SimpleFIN Bridge — paste the token at the hidden prompt
  ./cash-kit pull                  fetch balances + transactions into ledger/inbox
  ./cash-kit today                 render today.html
  ./cash-kit calendar              render spend-calendar.html
  ./cash-kit ledger                render ledger.html (searchable transactions, card tagging)
  ./cash-kit cards                 render cards.html (one page per card: a trip, a project, the business)
  ./cash-kit cards suggest|preview|add|plan|list|remove
                                   make cards from the command line — or let an AI do it (docs/AI.md)
  ./cash-kit morning               pull + today + calendar + ledger + cards (what the daily job runs)
  ./cash-kit ingest [--dry-run]    promote posted feed rows into ledger/transactions.csv
  ./cash-kit install               schedule 'morning' daily at 9:15 via launchd
  ./cash-kit open                  open today.html in your browser
  ./cash-kit status                show link + last pull
USAGE
  ;;
esac
